/*
 * 文件名：UserDaoImpl.java
 * 版权：Copyright by will_awoke
 * 描述：
 * 修改人：will_awoke
 * 修改时间：2014-8-18
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package com.qbgs.rbac.dao.impl;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import com.qbgs.base.Page;
import com.qbgs.rbac.dao.UserDao;


/**
 * 
 * @author will_awoke
 * @version 2014-8-18
 * @see UserDaoImpl
 * @since
 */
@Repository
public class UserDaoImpl implements UserDao
{

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public Page queryUserPage(int currentPage, int numPerPage, String loginName)
    {
        // 查询参数值集合
        List<Object> queryParamVals = null;

        StringBuffer sql = new StringBuffer();
        sql.append(" select user_id,login_name,email,real_name,state,remarks,create_time from t_sys_user where 1=1 ");

        if (!StringUtils.isBlank(loginName))
        {
            sql.append(" and instr(login_name,?)>0 ");
            queryParamVals = new LinkedList<Object>();
            queryParamVals.add(loginName);
        }
        sql.append(" order by user_id desc");

        return new Page(sql.toString(), queryParamVals, currentPage, numPerPage, jdbcTemplate,
            true);
    }

    @Override
    public int updateUserPwd(String loginName, String newPwd)
    {
        String sql = " update t_sys_user t set t.password=? where t.login_name=? ";
        Object[] paras = {newPwd, loginName};

        return jdbcTemplate.update(sql, paras);
    }

    @Override
    public int addUser(final String loginName, final String password, final String realName,
                       final String email, final String state, final String remarks)
    {
        KeyHolder keyHolder = new GeneratedKeyHolder();
        jdbcTemplate.update(new PreparedStatementCreator()
        {
            public PreparedStatement createPreparedStatement(Connection conn)
                throws SQLException
            {
                String sql = " insert into t_sys_user(login_name,password,real_name,email,state,remarks,create_time) values(?,?,?,?,?,?,SYSDATE()) ";
                PreparedStatement pstat = conn.prepareStatement(sql);
                pstat.setString(1, loginName);
                pstat.setString(2, password);
                pstat.setString(3, realName);
                pstat.setString(4, email);
                pstat.setString(5, state);
                pstat.setString(6, remarks);
                return pstat;
            }
        }, keyHolder);

        return keyHolder.getKey().intValue();
    }

    @Override
    public int addUserRole(String userId, String roleId)
    {
        String sql = " insert into t_user_role(user_id,role_id) values(?,?) ";
        Object[] paras = {userId, roleId};

        return jdbcTemplate.update(sql, paras);
    }

    @Override
    public int queryByLoginName(String loginName)
    {
        String sql = " select count(1) from t_sys_user where login_name=? ";
        Object[] paras = {loginName};

        return jdbcTemplate.queryForInt(sql, paras);
    }

    @Override
    public List<String> queryRoleIdsByUserId(String userId)
    {
        String sql = " select role_id from t_user_role where user_id=? ";
        Object[] paras = {userId};

        return jdbcTemplate.queryForList(sql, paras, String.class);
    }

    @Override
    public int deleteUser(String userId)
    {
        String sql = " update t_sys_user set state=0 where user_id=? ";
        Object[] paras = {userId};

        return jdbcTemplate.update(sql, paras);
    }

    @Override
    public int deleteRolesOfUser(String userId)
    {
        String sql = " delete from t_user_role where user_id=? ";
        Object[] paras = {userId};

        return jdbcTemplate.update(sql, paras);
    }

    @Override
    public int updateUser(String userId, String loginName, String realName, String email,
                          String remarks)
    {
        String sql = " update t_sys_user set real_name=?, email=?, remarks=? where user_id=? ";
        Object[] paras = {realName, email, remarks, userId};

        return jdbcTemplate.update(sql, paras);
    }

}
